package com.SeleniumTestsInternetExplorer;

import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.SeleneseTestBase;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.regex.Pattern;

import com.thoughtworks.selenium.SeleneseTestBase;

public class ViewAndPrintBoadingPassTestCase extends SeleneseTestBase {

	@Before
	public void setUp() throws Exception {
		File file = new File(
				"C:/Users/Steven/Documents/4th Year/EECE 419/IEDriverServer.exe");
		System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
		WebDriver driver = new InternetExplorerDriver();
		String baseUrl = "http://localhost:8080/Website/";
		selenium = new WebDriverBackedSelenium(driver, baseUrl);
	}
	
	// Created by Steven Chow
	// Scenario 1: Boarding Pass Successfully Displayed
	// Test Case 1: User requests viewing boarding pass haveing a valid
	// ticket on their profile
	@Test
	public void testViewBoardingPassSC1TC1() throws Exception {
		// Go to website
		selenium.open("");
		// Login
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=username", "User9");
		selenium.type("name=password", "pass9");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		assertEquals("Customer Portal", selenium.getTitle());
		// Search for a Flight
		selenium.click("css=div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// Search without using the filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Pick the 1st one
		selenium.click("name=r1");
		// Reserve
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in name, seat and choose meal
		selenium.type("name=firstName", "Blue");
		selenium.type("name=lastName", "Bear");
		selenium.type("name=seatID", "A19A");
		selenium.click("css=label.radio > span");
		selenium.click("name=mealOption");
		// Proceed to Payment
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		// Use credit Card for all reservation tests
		selenium.click("name=credit");
		// Enter in correct value
		selenium.type("name=credit", "100");
		// Make the payment
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are back in the Customer portal page
		assertEquals("Customer Portal", selenium.getTitle());
		// Check if there is a message saying that a reservation has been added.
		assertEquals("1 new reservation added!",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/h4"));
		// Check if there is a View Boarding Pass Button
		assertTrue(selenium
				.isElementPresent("css=div.tile.bg-color-purple > div.tile-content.icon"));
		selenium.click("css=div.tile.bg-color-purple > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		assertEquals("View Boarding Pass", selenium.getTitle());
		// Check if our reservation is there
		assertEquals(
				"",
				selenium.getText("//html/body/div/div[2]/div/form/table/tbody/tr/td[2]/img"));
		assertEquals(
				"Blue Bear",
				selenium.getText("//html/body/div/div[2]/div/form/table/tbody/tr/td[3]"));
		assertEquals(
				"A19A",
				selenium.getText("//html/body/div/div[2]/div/form/table/tbody/tr/td[6]"));
		assertEquals(
				"A1",
				selenium.getText("//html/body/div/div[2]/div/form/table/tbody/tr/td[7]"));
		selenium.click("name=r1");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we have all the information filled
		assertEquals("Blue", selenium.getText("//html/body/div/font"));
		assertEquals("Bear", selenium.getText("//html/body/div[2]/font"));
		assertEquals("Vancouver", selenium.getText("//html/body/div[3]/font"));
		assertEquals("1000", selenium.getText("//html/body/div[4]/font"));
		assertEquals("Los Angeles", selenium.getText("//html/body/div[5]/font"));
		assertEquals("A1", selenium.getText("//html/body/div[7]/font"));
		assertEquals("13:00", selenium.getText("//html/body/div[8]/font"));
		assertEquals("Blue", selenium.getText("//html/body/div[9]/font"));
		assertEquals("Bear", selenium.getText("//html/body/div[10]/font"));
		assertEquals("2012-10-09 13:00",
				selenium.getText("//html/body/div[11]/font"));
		assertEquals("Economy", selenium.getText("//html/body/div[12]/font"));
		assertEquals("A19A", selenium.getText("//html/body/div[13]/font"));
	}

	// Created by Steven Chow
	// Scenario 2: Boarding Pass unsuccessfully displayed
	// Test Case 1: User Requrests viewing boarding pass without having a
	// valid ticket on their profile
	@Test
	public void testViewBoardingPassSC2TC1() throws Exception {


		// Go to website
		selenium.open("http://localhost:8080/Website/");
		// Login
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=username", "User10");
		selenium.type("name=password", "pass10");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		assertEquals("Customer Portal", selenium.getTitle());
		// View Boarding Pass
		selenium.click("css=div.tile.bg-color-purple > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// Am i on the right page?
		assertEquals("View Boarding Pass", selenium.getTitle());
		// Check if there is a message
		assertTrue(selenium
				.isTextPresent("There are no Boarding Passes to view."));
	}
	
	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}
}
